#-------------------------------------------------------------------------------
# Name:        Credits
# Purpose:
#
# Author:      Pablo
#
# Created:     29/04/2013
# Copyright:   (c) Monxcleyr Productions 2013
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, config, os

class Credits():

  """basic credits"""

  def __init__(self,common, music):
    self.common = common
    self.music = music
    self.surface = pygame.Surface(self.common.initsize).convert_alpha()
    conf = config.Config()
    conf.read(os.path.join("resources/configs/credits.ini"))
    self.bg = conf.get('General', 'background')
    self.mask = conf.get('General', 'mask')
    if self.mask == 'None':
      self.mask = None
    self.fademask = conf.get('General', 'fademask')
    if self.fademask == 'None':
      self.fademask = None
    if self.bg != 'None':
      self.bg = pygame.image.load('resources/graphics/credits/' + self.bg)
      self.bg.convert_alpha()
    self.bgpos = conf.getint_tuple('General', 'backgroundpos')
##    if common.settings.quality > 3:
##      self.fademask = None
    if self.fademask:
      self.fademask = pygame.image.load('resources/graphics/credits/' + self.fademask).convert_alpha()
    if self.mask:
      self.mask = pygame.image.load('resources/graphics/credits/' + self.mask).convert_alpha()

    self.start = conf.get('Start', 'image')
    self.start = pygame.image.load('resources/graphics/credits/' + self.start).convert_alpha()
    self.startduration = conf.getint('Start', 'duration')
    self.startpos = conf.getint_tuple('Start', 'position')
    self.fadepos = 0

    self.credits = conf.get('Credits', 'image')
    self.credits = pygame.image.load('resources/graphics/credits/' + self.credits)
    self.creditspos = conf.getint_tuple('Credits', 'position')
    self.speed = conf.getfloat('Credits', 'speed')
    self.speed = self.speed / float(100)


    self.end = conf.get('End', 'image')
    self.end = pygame.image.load('resources/graphics/credits/' + self.end).convert_alpha()
    self.endstop = conf.getint('End', 'stop')
    self.endduration = conf.getint('End', 'duration')

    self.musicloop = conf.get('General', 'musicloop')
    if self.musicloop == 'None':
      self.musicloop = None

    self.maskpos = 0
    self.creditsoffset = 0

    self.prevtime = None
    self.endfadepos = 0
    self.endtime = None
    self.time = 0
    self.time2 = 0
    self.prevtime2 = 0
    self.running = True



  def draw(self, starfield, time, time2):


    time = int(time) / 1000

    if self.prevtime == None:
      self.prevtime = time
      self.prevtime2 = time2

    self.time = int(time - self.prevtime)

    if self.common.keyState.check(self.common.key.bind['confirm'])[0] == True:
      self.running = False

    if self.common.keyState.check(self.common.key.bind['escape'])[0] == True:
      self.running = False

  # Load music queue for menu if enabled
    if self.music.getCurrentLoop() != self.musicloop:
      self.music.stop()
      self.music.loadQueue(self.musicloop)
      self.music.playQueue()
    # This is boilerplate code for running the music queue.
    if self.musicloop:
      if pygame.mixer.music.get_busy() == 0:
        self.music.playQueue()


    self.surface.fill((0,0,0))

    starfield.draw(self.surface, self.common)

    if self.bg != 'None':
      self.surface.blit(self.bg, (0,0))


    if self.time < self.startduration:
      self.surface.blit(self.start, self.startpos)
    else:
      if abs(self.fadepos) < self.fademask.get_height() - self.start.get_height():
        startmasked = self.start.copy()
        startmasked.blit(self.fademask, (0, self.fadepos), None, pygame.BLEND_RGBA_MULT)
        self.fadepos -= 5

        self.surface.blit(startmasked, self.startpos)
      else:



        if self.time > self.startduration:
          pos = (self.creditspos[0], self.creditspos[1] + self.creditsoffset)
          maskpos = abs(self.creditsoffset) - self.common.initsize[1]
          masked = self.credits.copy()
          if self.mask:
            masked.blit(self.mask, (0, maskpos), None, pygame.BLEND_RGBA_MULT)

          self.creditsoffset += self.speed * float(time2)
          self.surface.blit(masked, pos)

          endpos = (self.creditspos[0], pos[1] + self.credits.get_height())
          if endpos[1] >= self.endstop:
            maskedend = self.end.copy()
            maskedendpos = maskpos - self.credits.get_height()
            if self.mask:
              maskedend.blit(self.mask, (0, maskedendpos), None, pygame.BLEND_RGBA_MULT)
            self.surface.blit(maskedend, endpos)
          else:
            if pos[1] + self.credits.get_height() >= 0:
              self.surface.blit(self.end, (self.creditspos[0], self.endstop))
            else:
              if self.endtime == None:
                self.endtime = time
              if time - self.endtime < self.endduration:
                self.surface.blit(self.end, (self.creditspos[0], self.endstop))
              else:
                if abs(self.endfadepos) < self.fademask.get_height() - self.start.get_height():
                  endmasked = self.end.copy()
                  endmasked.blit(self.fademask, (0, self.endfadepos), None, pygame.BLEND_RGBA_MULT)
                  self.endfadepos -= 5

                  self.surface.blit(endmasked, (self.creditspos[0], self.endstop))
                else:
                  self.running = False


    return self.surface


  def reset(self):

    self.prevtime = None
    self.fadepos = 0
    self.maskpos = 0
    self.endtime = None
    self.time = 0
    self.running = True
    self.endfadepos = 0
    self.creditsoffset = 0
    self.time2 = 0
    self.prevtime2 = 0


